/**
 * Mock数据配置
 * 仅在开发环境使用
 * 使用方法：在main.js中引入此文件即可
 */

import Mock from 'mockjs'

// Mock开关（可以通过配置文件控制）
const MOCK_ENABLED = false

if (process.env.NODE_ENV === 'development' && MOCK_ENABLED) {
  // 延迟响应，模拟网络请求
  Mock.setup({
    timeout: '200-600'
  })

  // ==================== 用户模块 ====================
  
  // 用户登录
  Mock.mock(/\/api\/user\/login/, 'post', (options) => {
    const body = JSON.parse(options.body)
    console.log('[Mock] 登录请求:', body)
    
    // 模拟登录验证
    if (!body.username || !body.password) {
      return {
        code: 400,
        message: '用户名和密码不能为空',
        data: null
      }
    }
    
    return {
      code: 200,
      message: '登录成功',
      data: {
        token: 'mock_token_' + new Date().getTime(),
        user: {
          id: 1,
          username: body.username,
          phone: '13800138000',
          role: body.username === 'admin' ? 2 : (body.username === 'landlord' ? 1 : 0)
        }
      }
    }
  })

  // 用户注册
  Mock.mock(/\/api\/user\/register/, 'post', (options) => {
    console.log('[Mock] 注册请求:', JSON.parse(options.body))
    return {
      code: 200,
      message: '注册成功',
      data: null
    }
  })

  // 获取当前用户信息
  Mock.mock(/\/api\/user\/info/, 'get', {
    code: 200,
    message: '成功',
    data: {
      id: 1,
      username: '@word(5,10)',
      phone: /^1[3-9]\d{9}$/,
      email: '@email',
      realName: '@cname',
      'role|0-2': 0,
      status: 0,
      avatar: 'https://i.pravatar.cc/200',
      createTime: '@datetime'
    }
  })

  // 更新用户信息
  Mock.mock(/\/api\/user\/update/, 'put', {
    code: 200,
    message: '更新成功',
    data: null
  })

  // 获取用户统计信息
  Mock.mock(/\/api\/user\/baseInfoCount/, 'get', {
    code: 200,
    message: '查询成功',
    data: {
      'visited|0-50': 1,
      'collection|0-30': 1,
      'myRent|0-5': 1,
      'myHouse|0-20': 1,
      'messageCount|0-10': 1
    }
  })

  // ==================== 房源模块 ====================

  // 房源列表查询
  Mock.mock(/\/api\/house\/list\?.*/, 'get', {
    code: 200,
    message: '成功',
    data: {
      total: 100,
      pageNo: 1,
      pageSize: 10,
      pages: 10,
      'list|10': [{
        'id|+1': 1,
        title: '@ctitle(10, 20)',
        address: '@city(true)@county(true)@cword(3,5)路@natural(1,200)号',
        'price|1000-10000.2': 1000,
        'area|20-200.2': 20,
        roomType: '@pick(["一室一厅", "两室一厅", "三室两厅", "四室两厅", "复式", "别墅"])',
        mainPic: function() {
          return `https://picsum.photos/400/300?random=${Math.random()}`
        },
        'viewCount|0-1000': 100,
        status: 1,
        createTime: '@datetime'
      }]
    }
  })

  // 房源详情
  Mock.mock(/\/api\/house\/\d+/, 'get', (options) => {
    const id = parseInt(options.url.match(/\/api\/house\/(\d+)/)[1])
    
    return {
      code: 200,
      message: '成功',
      data: {
        id: id,
        landlordId: 2,
        landlordName: '@cname',
        title: '@ctitle(10, 20)',
        description: '@cparagraph(3, 7)',
        address: '@city(true)@county(true)@cword(3,5)路@natural(1,200)号',
        province: '@province',
        city: '@city',
        district: '@county',
        'price|2000-8000.2': 2000,
        'area|40-150.2': 40,
        roomType: '@pick(["一室一厅", "两室一厅", "三室两厅"])',
        'floor|1-30': 1,
        'totalFloor|10-33': 10,
        orientation: '@pick(["南", "北", "东", "西", "南北"])',
        decoration: '@pick(["毛坯", "简装", "精装", "豪华"])',
        facilities: ['空调', '洗衣机', '冰箱', '热水器', '床', '衣柜', 'WiFi', '燃气'],
        'picList|3-6': [{
          'id|+1': 1,
          url: function() {
            return `https://picsum.photos/800/600?random=${Math.random()}`
          },
          'isMain|0-1': 0
        }],
        status: 1,
        auditStatus: 1,
        'viewCount|50-500': 50,
        createTime: '@datetime'
      }
    }
  })

  // 发布房源
  Mock.mock(/\/api\/house\/publish/, 'post', {
    code: 200,
    message: '发布成功，等待审核',
    data: {
      id: '@increment',
      status: 0,
      auditStatus: 0
    }
  })

  // 更新房源
  Mock.mock(/\/api\/house\/update/, 'put', {
    code: 200,
    message: '更新成功',
    data: null
  })

  // 删除房源
  Mock.mock(/\/api\/house\/\d+/, 'delete', {
    code: 200,
    message: '删除成功',
    data: null
  })

  // ==================== 预约模块 ====================

  // 创建看房预约
  Mock.mock(/\/api\/appointment\/create/, 'post', {
    code: 200,
    message: '预约成功',
    data: {
      id: '@increment',
      status: 0
    }
  })

  // 我的预约列表
  Mock.mock(/\/api\/appointment\/myList/, 'post', {
    code: 200,
    message: '成功',
    data: {
      'total|20-50': 20,
      pageNo: 1,
      pageSize: 10,
      'pages|2-5': 2,
      'list|10': [{
        'id|+1': 1,
        'houseId|1-100': 1,
        houseTitle: '@ctitle(10, 20)',
        houseAddress: '@county(true)',
        appointmentTime: function() {
          // 生成未来的时间
          return Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
        },
        'status|0-3': 0,
        createTime: '@datetime'
      }]
    }
  })

  // 房东预约列表
  Mock.mock(/\/api\/appointment\/landlordList/, 'post', {
    code: 200,
    message: '成功',
    data: {
      'total|15-40': 15,
      pageNo: 1,
      pageSize: 10,
      'pages|2-4': 2,
      'list|10': [{
        'id|+1': 1,
        'userId|1-100': 1,
        userName: '@cname',
        'houseId|1-50': 1,
        houseTitle: '@ctitle(10, 20)',
        appointmentTime: '@datetime',
        contactPhone: /^1[3-9]\d{9}$/,
        remark: '@csentence',
        'status|0-3': 0,
        createTime: '@datetime'
      }]
    }
  })

  // 处理预约
  Mock.mock(/\/api\/appointment\/handle/, 'put', {
    code: 200,
    message: '处理成功',
    data: null
  })

  // 取消预约
  Mock.mock(/\/api\/appointment\/cancel/, 'put', {
    code: 200,
    message: '取消成功',
    data: null
  })

  // ==================== 租房申请模块 ====================

  // 创建租房申请
  Mock.mock(/\/api\/apply\/create/, 'post', {
    code: 200,
    message: '申请成功',
    data: {
      id: '@increment',
      status: 0
    }
  })

  // 我的申请列表
  Mock.mock(/\/api\/apply\/myList/, 'post', {
    code: 200,
    message: '成功',
    data: {
      'total|10-30': 10,
      pageNo: 1,
      pageSize: 10,
      'pages|1-3': 1,
      'list|5': [{
        'id|+1': 1,
        'houseId|1-100': 1,
        houseTitle: '@ctitle(10, 20)',
        houseAddress: '@county(true)',
        rentStartTime: '@date',
        rentEndTime: '@date',
        'status|0-3': 0,
        createTime: '@datetime'
      }]
    }
  })

  // 处理申请
  Mock.mock(/\/api\/apply\/handle/, 'put', {
    code: 200,
    message: '处理成功',
    data: null
  })

  // ==================== 收藏模块 ====================

  // 收藏房源
  Mock.mock(/\/api\/collection\/add/, 'post', {
    code: 200,
    message: '收藏成功',
    data: null
  })

  // 取消收藏
  Mock.mock(/\/api\/collection\/\d+/, 'delete', {
    code: 200,
    message: '取消收藏成功',
    data: null
  })

  // 检查是否已收藏
  Mock.mock(/\/api\/collection\/check\/\d+/, 'get', {
    code: 200,
    message: '成功',
    data: {
      'isCollected|1': [true, false]
    }
  })

  // 我的收藏列表
  Mock.mock(/\/api\/collection\/myList/, 'post', {
    code: 200,
    message: '成功',
    data: {
      'total|10-30': 10,
      pageNo: 1,
      pageSize: 10,
      'pages|1-3': 1,
      'list|10': [{
        'id|+1': 1,
        'houseId|1-100': 1,
        houseTitle: '@ctitle(10, 20)',
        houseAddress: '@county(true)',
        'housePrice|2000-8000.2': 2000,
        mainPic: function() {
          return `https://picsum.photos/400/300?random=${Math.random()}`
        },
        createTime: '@datetime'
      }]
    }
  })

  // ==================== 消息模块 ====================

  // 消息列表
  Mock.mock(/\/api\/message\/list/, 'post', {
    code: 200,
    message: '成功',
    data: {
      'total|20-50': 20,
      pageNo: 1,
      pageSize: 10,
      'pages|2-5': 2,
      'list|10': [{
        'id|+1': 1,
        type: '@pick(["APPOINTMENT", "APPLY", "AUDIT", "SYSTEM"])',
        title: '@ctitle(5, 15)',
        content: '@cparagraph(1, 3)',
        'relatedId|1-100': 1,
        'isRead|0-1': 0,
        createTime: '@datetime'
      }]
    }
  })

  // 标记消息已读
  Mock.mock(/\/api\/message\/read\/\d+/, 'put', {
    code: 200,
    message: '标记成功',
    data: null
  })

  // 批量标记已读
  Mock.mock(/\/api\/message\/readBatch/, 'put', {
    code: 200,
    message: '标记成功',
    data: null
  })

  // 获取未读消息数
  Mock.mock(/\/api\/message\/unreadCount/, 'get', {
    code: 200,
    message: '成功',
    data: {
      'count|0-20': 5
    }
  })

  // 删除消息
  Mock.mock(/\/api\/message\/\d+/, 'delete', {
    code: 200,
    message: '删除成功',
    data: null
  })

  // ==================== 图片上传 ====================

  // 上传图片
  Mock.mock(/\/api\/pic\/upload/, 'post', {
    code: 200,
    message: '上传成功',
    data: function() {
      return `https://picsum.photos/800/600?random=${Math.random()}`
    }
  })

  // 上传头像
  Mock.mock(/\/api\/user\/uploadHead/, 'post', {
    code: 200,
    message: '上传成功',
    data: function() {
      return `https://i.pravatar.cc/200?random=${Math.random()}`
    }
  })

  console.log('%c[Mock] Mock数据已启用！', 'color: green; font-weight: bold;')
  console.log('%c[Mock] 如需关闭Mock，请修改 MOCK_ENABLED 为 false', 'color: orange;')
}

export default Mock

